<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://demo.thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\store\controller;

use library\Controller;
use library\tools\Data;
use think\Db;
use app\store\model\Common;
/**
 * 电站列表
 * Class Documentlist
 * @package app\store\controller
 */
class Station extends Controller
{
    /**
     * 指定数据表
     * @var string
     */
    protected $table = 'StorePowerStation';
    protected $table1 = 'store_power_station';

    /**
     * 电站列表
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {
        $this->title = '电站列表';

        $this->user_list = Db::table("store_member")->field("id,username")->select();
        $this->wuye = Db::query("select m.id,s.name username,s.phone from store_power_station s left join store_member m on m.id=s.user_id where m.is_admin=1 and s.type=1");
        $query = $this->_query($this->table)->like('name,address,phone')->equal("user_id,status,type");
        $query->where("delete_time is null")->order('id desc')->page();
    }

    /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _index_page_filter(&$data)
    {
        foreach ($data as &$vo) {
            $u_id = $vo['user_id'];
            $vo['user'] = Db::table("store_member")->where("id=$u_id")->find();
            $vo['img_arr'] = explode('|',$vo['images']);

            //1正常   2故障  3被占用  4停用
            if($vo['status']==1){  $vo['status_r'] = '<span style="color: green;font-weight: bold">正常</span>'; }
            if($vo['status']==2){  $vo['status_r'] = '<span style="color: red;font-weight: bold">故障</span>'; }
            if($vo['status']==3){  $vo['status_r'] = '<span style="color: orangered">被占用</span>'; }
            if($vo['status']==4){  $vo['status_r'] = '<span style="color: gray">停用</span>'; }

            if($vo['type']==1){  $vo['type_r'] = '<span style="color: green;font-weight: bold">物业</span>'; }
            if($vo['type']==2){  $vo['type_r'] = '<span style="color: red;font-weight: bold">个人</span>'; }
            if($vo['type']==3){  $vo['type_r'] = '<span style="color: orangered">联营电站</span>'; }
            if($vo['type']==4){  $vo['type_r'] = '<span style="color: gray">非联营电站</span>'; }

        }


    }

    /**
     * 添加
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function add()
    {
        $this->title = '添加';
        $this->user_list = Db::table("store_member")->field("id,username,phone")->where(['is_have_station'=>0])->select();
        $this->isAddMode = '1';
        $this->_form($this->table, 'form');
    }

    /**
     * 编辑
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function edit()
    {
        $this->title = '编辑';
        $this->isAddMode = '0';
        $this->_form($this->table, 'form');
    }

    /**
     * 表单数据处理
     * @param array $data
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    protected function _form_filter(&$data)
    {
        // 生成ID
        if (empty($data['id'])) $data['id'] = Db::table($this->table1)->order("id desc")->value('id')+1;
        if ($this->request->isGet()) {

        } elseif ($this->request->isPost()) {
            $data['create_at'] = date('Y-m-d H:i:s');

            if(!empty($data['city'])){
                if(!empty($data['city']['picker'])){
                    $data['picker'] = $data['city']['picker'];
                    $picker = explode('/',$data['picker']);
                    if(count($picker)==3){
                        $data['province'] = $picker[0];
                        $data['city'] = $picker[1];
                        $data['area'] = $picker[2];
                        $data['province_id'] = $data["provinceId"];
                        $data['city_id'] = $data["cityId"];
                        $data['area_id'] = $data["districtId"];
                    }else{
                        unset($data['city']);
                    }
                }else{
                    unset($data['city']);
                }
            }else{
                unset($data['city']);
            }

            if($data['type']!=2){
                if($data['parent_rate']>0){
                    $this->error('非个人不能设置上级分佣比例，请及时修改');return false;
                }
            }
            $total_rate = $data['parent_rate']+$data['platform_rate']+$data['station_rate'];
            if($total_rate!=100){
                $this->error('分佣比例设置之和不为100%，请及时修改');return false;
            }

            $res = Db::table($this->table1)->where(["id" => $data['id']])->find();
            $lat = $res['lat'];
            $lng = $res['lng'];
            Db::table("store_park")->where(['station_id'=>$data['id']])->update(['lat'=>$lat,'lng'=>$lng]);
            Db::table("store_device")->where(['station_id'=>$data['id']])->update(['lat'=>$lat,'lng'=>$lng]);

        }
    }

    /**
     * 表单结果处理
     * @param boolean $result
     */
    protected function _form_result($result)
    {
        if ($result && $this->request->isPost()) {

            $post = $this->request->post();
            Db::table("store_member")->where(["id" => $post['user_id']])->update(['is_have_station'=>1]);

            $this->success('编辑成功！', 'javascript:history.back()');
        }
    }

    /**
     * 修改状态
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function edit_ziduan(){
        $id = $_POST['id']??'';
        $ziduan = $_POST['ziduan']??'';
        $val = $_POST['val']??'';

        Db::query("update store_power_station set $ziduan='$val' where id=$id");
    }

}
